﻿using System;
using System.Collections;
using System.Web.UI.WebControls;
using System.Web.UI;
using Model;
using Resources;
using Service;


public partial class workOrderDetail : Page
{
    WorkOrder  _WorkOrder  = new WorkOrder();
    WorkOrderService _WorkOrderService = new WorkOrderService();
    ReferenceDataService _ReferenceService = new ReferenceDataService();
    IList listEmployeeById = new ArrayList();
    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (!IsPostBack)
        {
            int workOrderId = GetworkOrderId();
            // add to hide information and not to execute data 
            pnlBody.Visible = workOrderId == 0 ? false : true;
            if(workOrderId  == 0) return;
           
            if (workOrderId != 0) DisplayDetailInfo(workOrderId);
            else ClearWorkOrder();
           
            lblCodeNumber.Text = Resource.LabelWorkOrderModify + " " + workOrderId;
           
         }
    }
    private void ReloadData()
    {
        int workOrderId = GetworkOrderId();
        if (workOrderId != 0) DisplayDetailInfo(workOrderId);
        else ClearWorkOrder();

        lblCodeNumber.Text = Resource.LabelWorkOrderModify + " " + workOrderId;
        SetDisplayModeToComponents();
        txtWODescription.Text = ""; 
    }
    
    private void SetDisplayModeToComponents()
    {
        bool flagEnable = !String.IsNullOrEmpty(Request.QueryString["displayMode"])
                              ? Request.QueryString["displayMode"].Equals("true") ? false : true
                              :
                                  false;
        btnValidate.Visible = flagEnable;

    }

    public int GetworkOrderId()
    {
        if (Session["workOrderId"] != null) return Int32.Parse(Session["workOrderId"].ToString());
        return 0;
    }
    


    private void DisplayDetailInfo(int workOrderId)
    {
        _WorkOrder = _WorkOrderService.GetWorkOrder(workOrderId);
        if (_WorkOrder.WorkorderId != null &&  _WorkOrder.WorkorderId != 0 )
        {
            txtRequestDate.Text =
                UitilityService.ConvertDateToString(_WorkOrder.RequestDate, Resource.DateEnglishFormat);

            txtWorkOrderNumber.Text = _WorkOrder.WorkorderId.ToString();
            txtWODescription.Text = _WorkOrder.Description;
            if (_WorkOrder.RequesterId != 0)
            {
                Employee employee = new ReferenceDataService().GetEmployeeById(_WorkOrder.RequesterId);
                ListItem item = new ListItem(employee.EmployeeId.ToString(), employee.EmployeeName);

                if (drlEmployee.Items.Contains(item))
                    drlEmployee.SelectedValue = _WorkOrder.RequesterId.ToString();
            }

            drlCriteria.SelectedValue = _WorkOrder.CriteriaId.ToString();
            drlRequestType.SelectedValue = _WorkOrder.RequestMadeById.ToString();
            
            new ReferenceDataService().GetEmployeeById(_WorkOrder.EmployeeId);
            
            drlStatus.SelectedValue = (_WorkOrder.StatusId  != null) ? _WorkOrder.StatusId.ToString() : "-1";
            chkAuthorize.Checked = _WorkOrder.Authorized;
            txtRequestDate.Text = new Utility().ConvertToDisplayDateString(_WorkOrder.RequestDate);
            txtAgreeDate.Text = new Utility().ConvertToDisplayDateString(_WorkOrder.AgreedDate);
            txtReason.Text = _WorkOrder.CancelReason;
            drlDepartment.SelectedValue = _WorkOrder.Employee.DepartmentId.ToString();
            


        }
        else
        {
            ClearWorkOrder();
        }
        
    }
    private bool ValidateDataWorkOrderPart()
    {

        if (drlDepartment.SelectedIndex == 0)
        {
            lblRequiredDepartment.Text = Resource.MessInformSelectAValue;
            return false;
        }
        lblRequiredDepartment.Text = string.Empty;

        if (drlEmployee.SelectedIndex == -1)
        {
            lblRequiredEmployee.Text = Resource.MessInformSelectAValue;
            return false;
        }
        lblRequiredEmployee.Text = string.Empty;

        if (drlRequestType.SelectedIndex == 0)
        {
            lblRequiredRequestTypes.Text = Resource.MessInformSelectAValue;
            return false;
        }
        lblRequiredRequestTypes.Text = string.Empty;

        if (drlCriteria.SelectedIndex == 0)
        {
            lblRequiredCriteria.Text = Resource.MessInformSelectAValue;
            return false;
        }
        lblRequiredCriteria.Text = string.Empty;
        if (String.IsNullOrEmpty(txtRequestDate.Text))
        {
            lblRequiredRequestDate.Text = Resource.MsgFillInformation;
            return false;
        }
        lblRequiredRequestDate.Text = string.Empty;
        
        return true;
    }
    private void ClearWorkOrder()
    {
        txtRequestDate.Text = string.Empty;
        txtWODescription.Text = string.Empty;
        txtWorkOrderNumber.Text = string.Empty;
        drlCriteria.SelectedIndex = -1;
        drlDepartment.SelectedIndex = -1;
        drlEmployee.SelectedIndex = -1;
        drlRequestType.SelectedIndex = -1;
        chkAuthorize.Checked = false;
        drlStatus.SelectedIndex = -1;
     }



    protected void btnValidate_Click(object sender, EventArgs e)
    {
        if (!new Utility().TestPermissions(Resource.PermissionEditWorkOrder))
        {
            lblMessageValidate.Text = Resource.Permission;
            ReloadData();
            return;
        }
		
        try
        {

            if (!ValidateData()) return;

            //update workorder 
            WorkOrder workOrder = GetWorkOrder();

            _WorkOrderService.SaveWorkOrder(workOrder);

            lblMessageValidate.Text = Resource.MessageValidate;

            
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private bool ValidateData()
    {

            if (drlStatus.SelectedIndex <= 0)
            {
                lblrequiredStatus.Text = Resource.MessInformSelectAValue;
                return false;
            }
            lblrequiredStatus.Text = string.Empty;
            if (drlDepartment.SelectedIndex <= 0)
            {
                lblRequiredDepartment.Text = Resource.MessInformSelectAValue;
                return false;
            }
            lblRequiredDepartment.Text = string.Empty;
            if (drlEmployee.SelectedIndex == -1)
            {
                lblRequiredEmployee.Text = Resource.MessInformSelectAValue;
                return false;
            }
            lblRequiredEmployee.Text = string.Empty;
            if (drlCriteria.SelectedIndex == -1)
            {
                lblRequiredCriteria.Text = Resource.MessInformSelectAValue;
                return false;
            }
            lblRequiredCriteria.Text = string.Empty;
           

            if (drlRequestType.SelectedIndex == -1)
            {
                lblRequiredRequestTypes.Text = Resource.MessInformSelectAValue;
                return false;
            }
            lblRequiredRequestTypes.Text = string.Empty;

            if (String.IsNullOrEmpty(txtRequestDate.Text))
            {
                txtRequestDate.Text = Resource.MsgFillInformation;
                return false;
            }
            lblRequiredRequestDate.Text = string.Empty;
            return true;
    }
    private WorkOrder GetWorkOrder()
    {
        WorkOrder workOrder = new WorkOrder();
        workOrder.CriteriaId = Int32.Parse(drlCriteria.SelectedValue);
        workOrder.RequestMadeById = Int32.Parse(drlRequestType.SelectedValue);
        workOrder.EmployeeId = Int32.Parse(drlEmployee.SelectedValue);
        workOrder.WorkorderId = GetworkOrderId();
        workOrder.Authorized = chkAuthorize.Checked;
        workOrder.RequestDate = new Utility().ConvertToCustomizedFormat(txtRequestDate.Text);
        workOrder.AgreedDate = new Utility().ConvertToCustomizedFormat(txtAgreeDate.Text);
        workOrder.RequestTime = DateTime.Parse(Resource.DateTimeNull);
        workOrder.Description = txtWODescription.Text;
        workOrder.CancelReason = txtReason.Text;
        if(drlStatus.SelectedIndex > 0)
        workOrder.StatusId = Int32.Parse(drlStatus.SelectedValue);

        // force authorized to false when work order status is cancel
        if (drlStatus.SelectedValue.Equals(Resource.TaskCancel)) workOrder.Authorized = false;

       
        return workOrder;

    }

    protected void drlStatus_SelectedIndexChanged(object sender, EventArgs e)
    {
        //txtReason.Visible = drlStatus.SelectedValue.Equals(Resource.TaskCancel) ? true : false;
        //lblReason.Visible = drlStatus.SelectedValue.Equals(Resource.TaskCancel) ? true : false;
    }
    protected void drlStatus_TextChanged(object sender, EventArgs e)
    {
        //txtReason.Visible = drlStatus.SelectedValue.Equals(Resource.TaskCancel) ? true : false;
        //lblReason.Visible = drlStatus.SelectedValue.Equals(Resource.TaskCancel) ? true : false;
    }
}
